package cn.driver.manager.mapper;

import cn.driver.common.pojo.Admin;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminMapper {

    @Select("SELECT * FROM admin WHERE adminName=#{adminName} AND `password`=#{password}")
    Admin getAdmin(String adminName, String password);

    @Select({"<script>",
            "Select account,password,adminName,state from admin",
            "<where>",
            "          <if test='prm!=null'>",
            "              concat(adminName,state) like concat('%',#{prm},'%')",
            "              ",
            "          </if>",
            "   </where>",
            " limit #{pageNo},#{pageSize}",
            "</script>"})
    List<Admin> getAdminList(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("prm") String prm);

    @Select({"<script>",
            " SELECT count(1) FROM admin",
            "   <where>",
            "          <if test='prm!=null'>",
            "              concat(adminName,state) like concat('%',#{prm},'%')",
            "              ",
            "          </if>",
            "   </where>",
            "</script>"})
    int selectAdmin(String prm);

    @Select("SELECT account,`password`,adminName,state FROM admin WHERE account=#{account}")
    Admin selectOne(int account);

    @Insert("INSERT INTO admin(account,`password`,adminName)VALUES(#{account},#{password},#{adminName})")
    int addAdmin(Admin admin);

    //@Update("UPDATE admin SET account = #{account},`password` = #{password},adminName = #{adminName},state = #{state} WHERE account = #{account};")
    @Update({"<script>",
            "update admin",
            "   <set>",
            "       <if test='account != null and account != \"\"'>`account` = #{account},</if>",
            "       <if test='password != null and password != \"\"'>`password` = #{password},</if>",
            "       <if test='adminName != null and adminName != \"\"'>`adminName` = #{adminName},</if>",
            "       <if test='state != null and state != \"\"'>`state` = #{state},</if>",
            "   </set>",
            "where account = #{account}",
            "</script>"})
    int updateAdmin(Admin admin);

    @Delete("DELETE FROM admin WHERE account = #{account};")
    int delete(String account);
}
